package com.hc.threadpool.service.impl;

import com.hc.threadpool.service.ThreadPoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author: huangcheng
 * @CreateTime: 2023-09-06  11:01
 * @Description:
 * @Version: 1.0
 */
@Service
@Slf4j
public class ThreadPoolServiceImpl implements ThreadPoolService {

   @Autowired
   private ThreadPoolExecutor threadPoolExecutor;
   @Resource
   // @Qualifier("springTaskExecutor")
   private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    @Async
    public void testSpringExceptionThread() {
        log.info(Thread.currentThread().getName() + "   spring线程池异常测试");
        throw new RuntimeException("士大夫");
    }
    @Override
    @Async("springTaskExecutor")
    public void testSpringSyncThread() {
        log.info(Thread.currentThread().getName() + "   spring异步线程测试springTaskExecutor");
    }

    @Override
    public void testSpringThreadPool() {
       threadPoolTaskExecutor.execute(()->{
           log.info(Thread.currentThread().getName() + "   spring线程池执行testSpringThreadPool");
           // throw new RuntimeException("出现异常");
       });
    }


    @Override
    @Async("jdkTaskExecutor")
    public void testJdkSyncThread() {
        log.info(Thread.currentThread().getName() + "   jdk异步执行jdkTaskExecutor");
    }

    @Override
    public void testJdkThreadPool() {
       threadPoolExecutor.execute(() -> {
           log.info(Thread.currentThread().getName() + "   jdk线程池执行jdkTaskExecutor");
       });
    }

    /**
     * 开启@EnableAsync注解，不注入ThreadPoolTaskExecutor情况使用默认线程池SimpleAsyncTaskExecutor
     */
    @Override
    @Async
    public void testDefaultSpringThread() {
        log.info(Thread.currentThread().getName() + "spring默认线程池执行");

    }
}
